package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.EnMkt;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.service.EnMktService;
import lombok.extern.log4j.Log4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created by ShenYiChou on 2017/4/10.
 */
@Log4j
@Controller
@RequestMapping("/admin/EnMktStat")
public class EnMktStat {
    @Autowired
    private EnMktService enMktService;

    @RequestMapping("/index")
    public String index(Model model){
        return "EnMktStat";
    }

    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> enMktList(EnMkt enMkt){
        List<EnMkt> enMktList = enMktService.getEnMktPage(enMkt);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(enMktList);
        tableContainer.setiTotalDisplayRecords(enMkt.getTotalCount());
        tableContainer.setiTotalRecords(enMkt.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }
    @RequestMapping(value = "/restful",method = RequestMethod.POST)
    public ResponseEntity<?> enMktlike(EnMkt enMkt){
        List<EnMkt> enMktList = enMktService.getEnMktsIn7(enMkt);
        enMktList.addAll(enMktService.getEnMktsOut7(enMkt));
        return new ResponseEntity<>(enMktList, HttpStatus.OK);
    }
    @RequestMapping(value = "/trunEnMkt",method = RequestMethod.GET)
    public ResponseEntity<?> trunEnMkt(){
        boolean flag = enMktService.trunEnMkt();
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
    @RequestMapping(value = "/import")
    public ResponseEntity<?> importExcel(MultipartFile mktFile) {
        try {
            XSSFWorkbook xwb = new XSSFWorkbook(mktFile.getInputStream());
            XSSFSheet sheet = xwb.getSheetAt(0);
            XSSFRow row;
            EnMkt enMkt = new EnMkt();
            for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
                if (i == 0) {
                    continue;
                }
                enMkt.setId(null);
                row = sheet.getRow(i);
                if(Objects.equals(row.getCell(1).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                if(Objects.equals(row.getCell(2).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                enMkt.setOrg_name(row.getCell(1).toString());    //excel批量导入时只有两个数值：机构名 和 机构入市状态
                List<EnMkt> enMktList = this.enMktService.getEnMkt(enMkt);

                if(enMktList.isEmpty()) {
                    enMkt.setEnter_stat(row.getCell(2).toString());
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date=new Date();
                    String str=sdf.format(date);
                    enMkt.setEnter_time(str);
                    boolean flag = this.enMktService.newEnMkt(enMkt);
                    if (!flag) {
                        return new ResponseEntity<>(false, HttpStatus.OK);
                    }
                }else {
                    enMkt = enMktList.get(0);
                    enMkt.setEnter_stat(row.getCell(2).toString());
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date=new Date();
                    String str=sdf.format(date);
                    enMkt.setEnter_time(str);
                    boolean flag = this.enMktService.updateEnMkt(enMkt);
                    if (!flag) {
                        return new ResponseEntity<>(false, HttpStatus.OK);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<?> createOrUpdate(@Valid EnMkt enMkt, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String str=sdf.format(date);
        enMkt.setEnter_time(str);
        boolean flag = false;
        if(enMkt.getId()!=null && enMkt.getId()!=0){
            flag = this.enMktService.updateEnMkt(enMkt);
        }else {
            flag = this.enMktService.newEnMkt(enMkt);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteEnMkt(@PathVariable("id") int id){
        boolean flag = this.enMktService.deleteEnMktById(id);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }
}
